package net.shouyong.topbilliards.fragment;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import net.shouyong.topbilliards.R;
import net.shouyong.topbilliards.Utils;
import net.shouyong.topbilliards.bean.BilliardRecord;


import java.util.List;

/**
 * {@link RecyclerView.Adapter} that can display a {@link BilliardRecord} and makes a call to the
 */
public class HistoryBillirdrecordRecyclerViewAdapter extends RecyclerView.Adapter<HistoryBillirdrecordRecyclerViewAdapter.ViewHolder> {

    private final List<BilliardRecord> mValues;

    public HistoryBillirdrecordRecyclerViewAdapter(List<BilliardRecord> items) {
        mValues = items;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.fragment_billirdrecord_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        holder.mItem = mValues.get(position);
//        holder.mIdView.setText(mValues.get(position).id);
        holder.mIdView.setText(String.valueOf(holder.mItem.getIndex()));
        holder.startTimeView.setText(String.format(holder.startTimeView.getContext().getString(R.string.starttime_m), holder.mItem.getStartTime()));
        holder.endTimeView.setText(String.format(holder.endTimeView.getContext().getString(R.string.end_time), Utils.dateToString(holder.mItem.getEndTime())));
        holder.itemCost.setText(String.format(holder.itemCost.getContext().getString(R.string.item_cost), holder.mItem.getSettlement()));
    }

    @Override
    public int getItemCount() {
        return mValues.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public final View mView;
        public final TextView mIdView;
        public final TextView startTimeView;
        public final TextView endTimeView;
        public final TextView itemCost;
        public BilliardRecord mItem;

        public ViewHolder(View view) {
            super(view);
            mView = view;
            mIdView = (TextView) view.findViewById(R.id.id);
            startTimeView = (TextView) view.findViewById(R.id.start_time);
            endTimeView = (TextView) view.findViewById(R.id.end_time);
            itemCost = (TextView) view.findViewById(R.id.item_cost);
        }

        @Override
        public String toString() {
            return super.toString() + " '" + startTimeView.getText() + "'";
        }
    }
}
